//
//  ViewController.m
//  15-UICollectionLifeCycle
//
//  Created by 李昊林 on 16/8/3.
//  Copyright © 2016年 李昊林. All rights reserved.
//

#import "ViewController.h"
#import "CustomCollectionViewCell.h"
#define kCollectionViewCellReuse @"reuse"
#define kRandomColor  [UIColor colorWithRed:(arc4random() % 256) / 255.0f  green:(arc4random() % 256) / 255.0f  blue:(arc4random() % 256) / 255.0f  alpha:1]
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat width = (CGRectGetWidth(self.view.frame) - 5) / 2;
    
    layout.itemSize = CGSizeMake(width, 120);
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    [self.view addSubview:collectionView];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    [collectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewCellReuse];
    
}

//关于collectionView LifeCycle  
//1.numberOfSectionInCollection
//2.collectionView numberOfItemsInSection:
//3-5循环执行
//3.prepareForReuse(从重用池取出时调用)
//4.collectionView:forItemAtIndexPath
//5.collectionView:willDisolayCell
//6当cell完全出屏幕时 didEndplayingCell:
//iOS 10以前
//出屏幕时立即返回，依旧进入3-》4-》5
//iOS以后
//出屏幕立即返回，不会进入3提高了效率




#pragma mark -collectionLifeCycle
//区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    NSLog(@"[%@ : %s]",  self, __FUNCTION__);
    return 1;
}
//区里的cell数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSLog(@"[%@ : %s]",  self, __FUNCTION__);
    return 100;
}
//从重用池取cell
- ( __kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellReuse forIndexPath:indexPath];
    cell.backgroundColor = kRandomColor;
    NSLog(@"%@----%ld------[%@ : %s]", cell, indexPath.row, self, __FUNCTION__);
    return cell;
}
//Ios 8.0以后推出，cell将要显示在屏幕上时
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@----%ld------[%@ : %s]", cell, indexPath.row, self, __FUNCTION__);
}
//Ios 8.0以后推出，cell已经离开在屏幕上时
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@----%ld------[%@ : %s]", cell, indexPath.row, self, __FUNCTION__);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
